package com.palm.hubo2o.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.ColorInt;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.palm.palmutils.LayoutUtils;


/**
 * Created by dest1 on 2015/12/3.
 */
public class BetItemDecoration extends RecyclerView.ItemDecoration {
    private int mRadius;
    private final int mOvalColor;
    private final int mLineColor;
    private int mBackgroundColor;
    private Paint mPaint;
    private RectF mRectF;

    public BetItemDecoration(Context context, @ColorInt int topColor, @ColorInt int middleColor) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mRectF = new RectF();
        mLineColor = topColor;
        mOvalColor = middleColor;
        mRadius = LayoutUtils.dp2pix(context, 2.5f);
        mBackgroundColor = Color.WHITE;
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        final int top = 0;
        final int left = parent.getLeft();
        final int right = parent.getRight();
        final int bottom = top + parent.getPaddingTop();
        int radius = bottom - top;
        mRectF.set(left + radius / 2, top, right - radius / 2, bottom);
        mPaint.setColor(mLineColor);
        c.drawRoundRect(mRectF, radius, radius, mPaint);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin +
                    Math.round(ViewCompat.getTranslationY(child));
            final int bottom = top + mRadius * 2;
            final int translationX = Math.round(ViewCompat.getTranslationX(child));
            if (i == childCount - 1)
                drawLast(c, left + translationX, top, right + translationX, bottom);
            else drawNormal(c, left + translationX, top, right + translationX, bottom);

        }
    }

    private void drawNormal(Canvas cns, int left, int top, int right, int bottom) {
        mPaint.setColor(mBackgroundColor);
        cns.drawRect(left, top, right, bottom, mPaint);
        mPaint.setColor(mOvalColor);
        int index = left;
        while (index < right) {
            mRectF.set(index, top, index + mRadius * 2, bottom);
            cns.drawOval(mRectF, mPaint);
            index += mRadius * 3;
        }
    }

    private void drawLast(Canvas cns, int left, int top, int right, int bottom) {
        mPaint.setColor(mBackgroundColor);
        cns.drawRect(left, top, right, top + (bottom - top) / 2, mPaint);
        mPaint.setColor(mOvalColor);
        int index = left;
        while (index < right) {
            mRectF.set(index, top, index + mRadius * 2, bottom);
            cns.drawArc(mRectF, 180, 180, true, mPaint);
            index += mRadius * 3;
        }
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.set(0, 0, 0, mRadius * 2);
    }


}
